// pages/plan/plan.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    applyList: "",  // 列表
    isSubmit: false,  // 防止重复提交
  },

  // 删除
  deleteList: function(e){
    var cinemaId = e.currentTarget.dataset.id;
    var applyList = this.data.applyList;
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '您是否要删除改数据？',
      success: res => {
        if (res.confirm) {
          for (var i = 0; i < applyList.length; i++){
            if (cinemaId == applyList[i].id){
              applyList.splice(applyList[i],1);
              this.setData({
                applyList: applyList
              })
              app.globalData.planList = applyList;
              wx.setStorageSync("planList", applyList);
            }
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 提交申请
  submitApply: function(){
    wx.showLoading({
      title: '正在提交',
    });
    wx.request({
      url: app.globalData.url + '/order/add',
      method: "POST",
      data: {
        "access_token": app.globalData.token,
        "tel": app.globalData.userMobile,
        cinema_data: this.data.applyList
      },
      success: res => {
        if (res.data.err_code == "200") {
          app.showError("提交成功");
          app.globalData.planList = [];
          wx.clearStorage();

          this.setData({
            isSubmit: true,
            applyList: []
          })
          wx.switchTab({
            url: '../apply/apply',
          });
          
        } else {
          app.showError("失败，请联系客服");
        }
      },
      fail: res => {
        console.log(res);
        app.showError("服务器出错");
      }
    })
  },

  // 去申请页面
  goApply: function(){
    if (this.data.applyList == "") {
      app.showError("请先选择要投放的影院");
    }else{
      if(this.data.isSubmit == true){
        app.showError("请不要重复提交");
      }else{
        this.submitApply();
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.planList != ""){
      this.setData({
        applyList: app.globalData.planList
      });
    }else{
      wx.getStorage({
        key: 'planList',
        success: res => {
          if (res.data.length != 0){
            this.setData({
              applyList: res.data
            });
          }else{
            app.showError("暂无数据");
          }
        },
      });
    }
    this.setData({
      isSubmit: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.planList != "") {
      this.setData({
        applyList: app.globalData.planList
      });
      console.log(this.data.applyList)
    } else {
      wx.getStorage({
        key: 'planList',
        success: res => {
          this.setData({
            applyList: res.data
          });
        }
      });
    }
    this.setData({
      isSubmit: false
    })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '',
      path: '/page/index/index'
    }
  }
})